package array

import (
	mgl "github.com/go-gl/mathgl/mgl32"
)
//获取顶点法向量(索引缓存)
func GetIndicesNormal(points_ptr *[]float32,indices []uint32,slice_len int32,xi,yi,zi uint32){
	var points = *points_ptr
	var i uint32;
	var indices_len  = len(indices)
	f := func (j int,key int32) float32{
		value := indices[j]
		return points[int32(value)*slice_len+key]
	};
	getVec3 := func(j int) mgl.Vec3{
		return mgl.Vec3{f(j,0),f(j,1),f(j,2)}
	}
	//计算顶点法向量
	for i = 0;i<uint32(len(points)/int(slice_len));i++{
		first := mgl.Vec3{0,0,0}
		for j:=0;j<indices_len;j+=3{
			if indices[j] == i || indices[j+1] == i || indices[j+2] == i{
				p0 := getVec3(j)
				p1 := getVec3(j+1)
				p2 := getVec3(j+2)
				var vectx mgl.Vec3
				switch(i){
				case indices[j]:vectx = p0
				case indices[j+1]:vectx = p1
				case indices[j+2]:vectx = p2
				}
				normal := p1.Sub(p0).Cross(p2.Sub(p1)).Normalize()
				if normal.Dot(vectx) >0{
					normal = normal.Mul(-1)
				}
				first = first.Add(normal)
			}
		}
		points[i*uint32(slice_len)+xi] = first.X()
		points[i*uint32(slice_len)+yi] = first.Y()
		points[i*uint32(slice_len)+zi] = first.Z()
	}
}
